package org.csu.myPetStore.web.servlet.Login;

import org.csu.myPetStore.domain.Account;
import org.csu.myPetStore.service.LogcatService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.Date;

public class SignOutServlet extends HttpServlet {

    static final String MAIN = "/WEB-INF/jsp/catalog/Main.jsp";

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        Account account = (Account) session.getAttribute("account");

        LogcatService logcatService = new LogcatService();

        java.util.Date date = new java.util.Date();
        Date datesql = new Date(date.getTime());
        logcatService.InsertLogCat(account.getUsername(),account.getUsername() + " Sign Out  at " + datesql.toString());

        session.setAttribute("account",null);
        req.getRequestDispatcher(MAIN).forward(req,resp);
    }
}
